package model;

import java.io.IOException;
import javax.microedition.lcdui.Image;

/**
 * A factory for creating transportation objects. Only a single instance of this class may be
 * in memory at a time.
 *
 * @author rhaq
 * @version 1.00 2009-05-06 Initial submission.
 */
public class TransportationFactory
{
	/** The single instance of this factory to create. */
	private static TransportationFactory instance;

	/** The speed associated with the Honda Civic transportation method. */
	public static final int SPEED_HONDA_CIVIC = 2;

	public static final int SPEED_POLICE_CRUISER = 3;


	/**
	 * Initializes this factory object so transportation objects may be generated.
	 */
	private TransportationFactory()
	{
	}


	/**
	 * Creates a Honda Civic transportation object.
	 * @return a Honda Civic transportation object with its associated speed and image.
	 * @throws IOException If the graphic of the transportation object could not be loaded.
	 */
	public Transportation createHondaCivic() throws IOException
	{
		return createObject("/transport_hondaCivic.png", SPEED_HONDA_CIVIC);
	}


	/**
	 * Creates a Honda Civic transportation object.
	 * @return a Honda Civic transportation object with its associated speed and image.
	 * @throws IOException If the graphic of the transportation object could not be loaded.
	 */
	public Transportation createPoliceCruiser() throws IOException
	{
		return createObject("/copCruiser_small.png", SPEED_POLICE_CRUISER);
	}


	/**
	 * Creates a transportation object with the specified image source and the specified speed.
	 * @param imgSrc The source image to associate with this transportation object.
	 * @param speed The speed to associate with the transportation object.
	 * @return The transportation object with the specified image source and speed.
	 * @throws IOException If the graphic of the transportation object could not be loaded.
	 */
	private Transportation createObject(String imgSrc, int speed) throws IOException
	{
		Image image = Image.createImage(imgSrc);
		Transportation t = new Transportation(image, speed);

		return t;
	}


	/**
	 * Retrieves a single instance of this class.
	 * @return a single instance of this class.
	 */
	public static TransportationFactory getInstance()
	{
		if (instance == null)
			instance = new TransportationFactory();

		return instance;
	}
}